<template>
  <view class="container">
    <uni-nav-bar
      :border="false"
      statusBar
      title="详情"
      color="#000"
      left-icon="left"
      backgroundColor="transparent"
      @clickLeft="onBack"
    >
    </uni-nav-bar>

    <view class="box">
      <view class="mainBox">
        <view class="id"
          >柜号: {{ data?.frameLightNo }}, 编号: {{ data?.lightNo }}</view
        >
        <!-- <view class="val">灯柜状态: {{ $util.selectDictLabel(kdgData, data?.lightStatusKdg) }}</view>
                <view class="status">矿灯状态: {{ $util.selectDictLabel(kdData, data?.lightStatusKd) }}</view> -->
      </view>
      <view class="infoBox">
        <view class="item">
          <text class="name">灯柜状态</text>
          <text class="val">{{
            $util.selectDictLabel(kdgData, data?.lightStatusKdg)
          }}</text>
        </view>
        <view class="item">
          <text class="name">矿灯状态</text>
          <text class="val">{{
            $util.selectDictLabel(kdData, data?.lightStatusKd)
          }}</text>
        </view>
        <view class="item">
          <text class="name">姓名</text>
          <text class="val">{{ data?.empName }}</text>
        </view>
        <view class="item">
          <text class="name">所在部门</text>
          <text class="val">{{ data?.deptName }}</text>
        </view>
        <view class="item">
          <text class="name">更换次数</text>
          <text class="val">{{ data?.replaceNum }}</text>
        </view>
        <view class="item">
          <text class="name">矿灯领用投用时间</text>
          <text class="val">{{ data?.receiveDate }}</text>
        </view>
        <view class="item">
          <text class="name">强制报废到期时间</text>
          <text class="val">{{ data?.expireDate }}</text>
        </view>
        <view class="item">
          <text class="name">生产厂家</text>
          <text class="val">{{ data?.manufacturer }}</text>
        </view>
        <view class="item">
          <text class="name">安全标志</text>
          <text class="val">{{ data?.safetyMark }}</text>
        </view>
      </view>
    </view>
    <view class="footer">
      <button class="btn" type="primary" @click="onRecord">台账</button>
    </view>

    <Fab :data="fabData" @itemClick="onUpdate" />

    <uni-popup ref="popup">
      <FormView :data="data" :type="type" @callback="onCallback" />
    </uni-popup>
  </view>
</template>

<script>
import { getDeptList, getPersonList } from '@/api/index';
import {
  netLightFP,
  netLightEdit,
  netLightQXFP,
  getLightDetail,
  netLightExpire,
  netLightReplace,
  netLightReceive,
  netLightUnnormal,
  netLightMaintain,
} from '@/api/light';
import FormView from './form.vue';
import Fab from '@/components/Fab/index.vue';
import useAppStore from '@/store/modules/app';
export default {
  components: {
    Fab,
    FormView,
  },
  data() {
    return {
      // 灯柜状态
      kdgData: [
        {
          label: '在用',
          value: '1',
        },
        {
          label: '待分配',
          value: '2',
        },
        {
          label: '备用',
          value: '3',
        },
        {
          label: '接待',
          value: '4',
        },
      ],
      // 矿灯状态
      kdData: [
        {
          label: '正常',
          value: '1',
        },
        {
          label: '报废',
          value: '2',
        },
      ],
      data: {},
      query: {},
      // fab数据
      fabData: [],
      // 类型，因为表单不一样
      type: '',
      // store
      appStore: useAppStore(),
    };
  },
  onLoad(query) {
    this.query = query;
    this.getData();
    this.initData();
  },
  methods: {
    // 初始数据
    initData() {
      getDeptList().then((res) => {
        this.dData = res.data.map((item) => {
          return {
            text: item.deptName,
            label: item.deptName,
            value: item.deptId,
            parentId: item.parentId,
          };
        });
        this.appStore.updateDeptData(this.dData);
      });
      getPersonList().then((res) => {
        this.pData = res.data.map((item) => {
          return {
            text: item.name,
            label: item.name,
            value: item.id,
            deptId: item.departmentId,
          };
        });
        this.appStore.updatePersonData(this.pData);
      });
    },
    getData() {
      getLightDetail(this.query).then((res) => {
        this.data = res.data;
        // 操作项控制展示
        this.fabData = [
          {
            text: '编辑',
            icon: 'compose',
            show: true,
            color: '#4caf50',
            value: 'edit',
          },
          {
            text: '更换',
            icon: 'refresh',
            show: res.data.lightStatusKdg == 1,
            color: '#3f51b5',
            value: 'replace',
          },
          {
            text: '报废',
            icon: 'close',
            show: res.data.lightStatusKdg == 1,
            color: '#f44336',
            value: 'expire',
          },
          {
            text: '维修',
            icon: 'gear',
            show: res.data.lightStatusKdg == 1 && res.data.lightStatusKd == 2,
            color: '#FBC02D',
            value: 'maintain',
          },
          {
            text: '非正常',
            icon: 'info',
            show: res.data.lightStatusKdg == 1 && res.data.lightStatusKd == 1,
            color: '#FF5252',
            value: 'unnormal',
          },
          {
            text: '领取',
            icon: 'personadd',
            show: res.data.lightStatusKdg == 2,
            color: '#009688',
            value: 'receive',
          },
          {
            text: '分配',
            icon: 'auth',
            show: !res.data.deptName && !res.data.empName,
            color: '#9C27B0',
            value: 'fp',
          },
          {
            text: '取消分配',
            icon: 'clear',
            show: !!res.data.deptName || !!res.data.empName,
            color: '#E91E63',
            value: 'qxfp',
          },
        ];
      });
    },
    // 台账
    onRecord() {
      console.log(this.data);
      // url参数
      let obj = {
        lightId: this.data.lightId,
        lightFrameId: this.data.lightFrameId,
      };
      let str = this.$util.jsonToUrlParam(obj);
      this.onLocation(`/pages/light/record?${str}`);
    },
    // 更新
    onUpdate(t) {
      this.type = t;
      this.$refs.popup.open('bottom');
    },
    // form回调
    onCallback(r) {
      let api;
      if (this.type === 'replace') {
        api = netLightReplace;
      } else if (this.type === 'expire') {
        api = netLightExpire;
      } else if (this.type === 'maintain') {
        api = netLightMaintain;
      } else if (this.type === 'unnormal') {
        api = netLightUnnormal;
      } else if (this.type === 'fp') {
        api = netLightFP;
      } else if (this.type === 'qxfp') {
        api = netLightQXFP;
      } else if (this.type === 'receive') {
        api = netLightReceive;
      } else if (this.type === 'edit') {
        api = netLightEdit;
      }
      console.log(r);
      // loading提示
      uni.showLoading({ title: '提交中' });
      let params = {
        ...this.data,
        ...r,
      };
      api(params).then((res) => {
        uni.hideLoading();
        uni.showToast({
          title: `操作成功`,
        });
        // 刷新详情
        this.getData();
        this.$refs.popup.close();
      });
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - 160rpx);
  padding-bottom: calc(
    var(--window-bottom) + constant(safe-area-inset-bottom) + 160rpx
  );
  padding-bottom: calc(
    var(--window-bottom) + env(safe-area-inset-bottom) + 160rpx
  );
  background: linear-gradient(180deg, #d7e4fd 0%, #fff 50%);
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
  .box {
    margin: 50rpx 36rpx 0;
    padding: 24rpx;
    background: #ffffff;
    border-radius: 12rpx;
    box-shadow: 0 0 30rpx 0 #ccc;
    .mainBox {
      text-align: center;
      padding: 40rpx 0;
      line-height: 1;

      .id {
        font-size: 38rpx;
        font-weight: bold;
        color: #4288ff;
        padding-bottom: 40rpx;
      }

      .val {
        font-size: 40rpx;
        font-weight: 800;
        color: #ff884d;
        padding-bottom: 27rpx;

        text {
          font-size: 30rpx;
          margin-left: 10rpx;
        }
      }

      .status {
        font-size: 26rpx;
        font-weight: 500;
        color: #555555;
      }
    }
    .infoBox {
      .item {
        font-size: 30rpx;
        font-weight: 500;
        padding: 22rpx 12rpx;
        display: flex;
        justify-content: space-between;
        border-bottom: 2rpx solid #eaeaea;
        &:last-child {
          border-bottom: 0;
        }
        .name {
          color: #989898;
        }

        .val {
          width: 60%;
          color: #333;
          text-align: right;
          word-break: break-all;
        }
      }
    }
  }
  .myLabel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .star {
      color: #dd524d;
    }
  }
  .footer {
    display: flex;
    justify-content: space-evenly;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(
      var(--window-bottom) + constant(safe-area-inset-bottom) + 60rpx
    );
    bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom) + 60rpx);
    .btn {
      width: 40%;
      margin: 0;
    }
  }
}
</style>
